from tkinter import *
from tkinter.messagebox import *
from tkinter.filedialog import *
import os

win = Tk()
win.title("记事本")
win.geometry('500x500')


def author():
    showinfo("作者信息：", message="瑨宇编程")


def openFile():
    global fileName
    fileName = askopenfilename(defaultextension='.txt')  # 设置文件打开格式
    if fileName == "":
        fileName = None
    else:
        win.title("文件名：" + os.path.basename(fileName))
        txtPad.delete("1.0", "end")  # 删除原来文本框的内容
        f = open(fileName, "r", encoding="utf-8")
        txtPad.insert("1.0", f.read())  # 插入数据
        f.close()


def newFile():
    global fileName
    win.title("未命名")
    fileName = None
    txtPad.delete("1.0", "end")


def save():
    global fileName
    try:
        fileW = open(fileName, "w", encoding="utf-8")
        msg = txtPad.get("1.0", "end")
        fileW.write(msg)
        fileW.close()
    except:
        saves()


def saves():  # 另存为
    global fileName
    # asksaveasfilename 保存文件，默认文件名【未命名.txt】，保存格式【.txt】
    f = asksaveasfilename(initialfile="未命名.txt", defaultextension=".txt")
    fileName = f
    fileW = open(f, "w", encoding="utf-8")
    msg = txtPad.get("1.0", "end")
    fileW.write(msg)
    fileW.close()
    win.title("文件名：" + os.path.basename(fileName))


def clear():
    txtPad.delete("1.0", "end")  # 删除原来文本框的内容


def cut():
    # 剪切
    txtPad.event_generate("<<Cut>>")


def copy():
    # 复制
    txtPad.event_generate("<<Copy>>")


def paste():
    # 粘贴
    txtPad.event_generate("<<Paste>>")


def redo():
    # 重做
    txtPad.event_generate("<<Redo>>")


def undo():
    # 撤销
    txtPad.event_generate("<<Undo>>")


def selectAll():
    txtPad.tag_add("sel", "1.0", "end")


menubar = Menu(win)  # 创建菜单
win.config(menu=menubar)  # 设置菜单

fileMenu = Menu(menubar)  # 创建二级菜单
fileMenu.add_command(label="新键", accelerator="ctrl+n", command=newFile)  # accelerator设置快捷键
fileMenu.add_command(label="打开", accelerator="ctrl+o", command=openFile)  # accelerator设置快捷键
fileMenu.add_command(label="保存", accelerator="ctrl+s", command=save)  # accelerator设置快捷键
fileMenu.add_command(label="另存为", accelerator="ctrl+shift+s", command=saves)  # accelerator设置快捷键
menubar.add_cascade(label="文件", menu=fileMenu)  # 设置打开二级菜单的选项

editMenu = Menu(menubar)  # 创建二级菜单
editMenu.add_command(label="撤销", accelerator="ctrl+z", command=undo)  # accelerator设置快捷键
editMenu.add_command(label="重做", accelerator="ctrl+y", command=redo)  # accelerator设置快捷键
editMenu.add_separator()  # 分割线
editMenu.add_command(label="剪切", accelerator="ctrl+x", command=cut)  # accelerator设置快捷键
editMenu.add_command(label="复制", accelerator="ctrl+c", command=copy)  # accelerator设置快捷键
editMenu.add_command(label="粘贴", accelerator="ctrl+v", command=paste)  # accelerator设置快捷键
editMenu.add_command(label="全选", accelerator="ctrl+a", command=selectAll)  # accelerator设置快捷键
menubar.add_cascade(label="编辑", menu=editMenu)  # 设置打开二级菜单的选项

authorMenu = Menu(menubar)
authorMenu.add_command(label="作者", command=author)
menubar.add_cascade(label="关于", menu=authorMenu)
# fontMenu = Menu(menubar)
# fontMenu.add_command(label="宋体")

toolbar = Frame(win, height=25, bg="#E6E6FA")
openButton = Button(toolbar, text="打开", command=openFile)
openButton.pack(side=LEFT, padx=5, pady=5)
openButton = Button(toolbar, text="保存", command=save)
openButton.pack(side=LEFT, padx=10)
openButton = Button(toolbar, text="清空", command=clear)
openButton.pack(side=LEFT, padx=10)
toolbar.pack(expand=NO, fill=X)

# 创建文本框
txtPad = Text(win, undo=True, font=("宋体", 23))  # undo允许撤销
txtPad.pack(expand=YES, fill=BOTH)  # expan允许扩展
scroll = Scrollbar(txtPad)  # 设置滚动条
txtPad.config(yscrollcommand=scroll.set)
scroll.config(command=txtPad.yview())  # 滚动条竖装显示
scroll.pack(side=RIGHT, fill=Y)

win.mainloop()
